package com.project.XXXX.api;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.Servlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.omg.PortableServer.REQUEST_PROCESSING_POLICY_ID;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.aldjob.controllerImpl.ProfileController;
import com.aldjob.controllerImpl.UserController;
import com.google.gson.Gson;
import com.project.XXXX.inter.*;
import com.project.XXXX.model.*;
import com.project.XXXX.tools.ProjectTools;

import sun.misc.BASE64Decoder;

@Controller

public class Profile {

	@Resource
	private tbl_sysuserMapper tbl_sysuserMapper;
	@Resource
	private tbl_employeeinfoMapper tbl_employeeinfoMapper;
	@Resource
	private tbl_employerinfoMapper tbl_employerinfoMapper;
	boolean first;
	ProfileController uc;

	void prepare(HttpServletResponse response) {
		if (first) {
			// System.out.println("is first");
			uc = new ProfileController(tbl_sysuserMapper, tbl_employeeinfoMapper, tbl_employerinfoMapper);
			first = false;
		}
		response.setContentType("application/json;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
	}

	HttpSession getAppSession(String access_token) {
		SessionContext sessionContext = SessionContext.getInstance();
		return sessionContext.getSession(access_token);
	}

	public Profile() {
		first = true;
	}

	@RequestMapping("/api/user/profile/employee")
	public void ProfileSelectHandler(HttpServletRequest request, HttpServletResponse response, Model model,
			HttpSession session) {
		prepare(response);
		Map<String, String> map = new HashMap<String, String>();
		HttpSession sess = getAppSession(request.getParameter("access_token"));
		tbl_employeeinfo status = uc.ProfileSelectHandler(sess);
//		System.out.println(sess+" "+status);
		String json;
		if (status != null) {
			json = new Gson().toJson(status);
		}
		else{
			map.put("status", "failure");
			map.put("error_description", "please login");	
			json = new Gson().toJson(map);		
			response.setStatus(403);
		}			
		ProjectTools.ResponseOut(response, json);
	}

	@RequestMapping("/api/user/profile/employeeUpdate")
	public void ProfileUpdateHandler(HttpServletRequest request, HttpServletResponse response, Model model,
			HttpSession session) {
		prepare(response);
		Map<String, String> map = new HashMap<String, String>();
		HttpSession sess = getAppSession(request.getParameter("access_token"));
		int status = uc.ProfileUpdateHandler(request, sess);
		switch(status){
			case 200:
				map.put("status", "success");
				break;
			case 403:
				map.put("status", "failure");
				map.put("error_description", "wrong validateCode");
				break;
		}		
		response.setStatus(status);
		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}

	@RequestMapping("/api/user/profile/employerUpdate")
	public void FinishERInfo(HttpServletRequest request, HttpServletResponse response, Model model,
			HttpSession session) {
		prepare(response);
		Map<String, String> map = new HashMap<String, String>();
		HttpSession sess = getAppSession(request.getParameter("access_token"));
		int status = uc.FinishERInfo(request, sess);
		switch(status){
			case 200:
				map.put("status", "success");
				break;
			case 403:
				map.put("status", "failure");
				map.put("error_description", "wrong validateCode");
				break;
		}		
		response.setStatus(status);
		String json = new Gson().toJson(map);
		ProjectTools.ResponseOut(response, json);
	}

	@RequestMapping(value = "/api/user/profile/employer")
	public void ShowERInfo(HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session) {

		prepare(response);
		Map<String, String> map = new HashMap<String, String>();
		HttpSession sess = getAppSession(request.getParameter("access_token"));
		tbl_employerinfo status = uc.ShowERInfo(sess);
//		System.out.println(sess+" "+status);
		String json;
		if (status != null) {
			json = new Gson().toJson(status);
		}
		else{
			map.put("status", "failure");
			map.put("error_description", "please login");	
			json = new Gson().toJson(map);		
			response.setStatus(403);
		}			
		ProjectTools.ResponseOut(response, json);

	}

	@SuppressWarnings("restriction")
	@RequestMapping(value = "/api/user/profile/employerLogo.do", method = RequestMethod.POST)
	public void finishERLogoString(HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session,
			@RequestParam(value = "img") String image) {
		prepare(response);
		Map<String, String> map = new HashMap<String, String>();
		HttpSession sess = getAppSession(request.getParameter("access_token"));
		int status = uc.employerLogoString(request, sess, image);
		String json;
		switch(status){
		case 200:
			map.put("status", "success");
			break;
		case 403:
			map.put("status", "failure");
			map.put("error_description", "please log in");		
			response.setStatus(403);
		case 409:
			map.put("status", "failure");
			map.put("error_description", "logo update fail");		
			response.setStatus(403);
		}		
		json = new Gson().toJson(map);	
		ProjectTools.ResponseOut(response, json);
	}

	@SuppressWarnings("restriction")
	@RequestMapping(value = "/api/user/profile/employeeLogo.do", method = RequestMethod.POST)
	public void finishEELogoString(HttpServletRequest request, HttpServletResponse response, Model model, HttpSession session,
			@RequestParam(value = "img") String image) {
		prepare(response);
		Map<String, String> map = new HashMap<String, String>();
		HttpSession sess = getAppSession(request.getParameter("access_token"));
		int status = uc.employeeLogoString(request, sess, image);
		String json;
		switch(status){
		case 200:
			map.put("status", "success");
			break;
		case 403:
			map.put("status", "failure");
			map.put("error_description", "please log in");		
			response.setStatus(403);
		case 409:
			map.put("status", "failure");
			map.put("error_description", "logo update fail");		
			response.setStatus(403);
		}			
		json = new Gson().toJson(map);	
		ProjectTools.ResponseOut(response, json);
	}
}